package com.wu.service.impl;

import com.wu.mappers.ImagesMapper;
import com.wu.mappers.LabelsMapper;
import com.wu.mappers.MaterialsMapper;
import com.wu.pojo.Images;
import com.wu.pojo.Labels;
import com.wu.pojo.vo.MaterialsVo;
import com.wu.pojo.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.wu.pojo.Products;
import com.wu.mappers.ProductsMapper;
import com.wu.service.ProductsService;

import java.util.List;

/**
@author sun
@date 2022/3/27 - 23:05
*/
@Service
public class ProductsServiceImpl implements ProductsService{

    @Resource
    private ProductsMapper productsMapper;
    @Autowired
    private ImagesMapper imagesMapper;
    @Autowired
    private LabelsMapper labelsMapper;
    @Autowired
    private MaterialsMapper materialsMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return productsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Products record) {
        return productsMapper.insert(record);
    }

    @Override
    public int insertSelective(Products record) {
        return productsMapper.insertSelective(record);
    }

    @Override
    public Products selectByPrimaryKey(Integer id) {
        return productsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Products record) {
        return productsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Products record) {
        return productsMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<ProductVo> findProductsVo(Products products) {
        //查询产品列表
        List<ProductVo> productVoList=productsMapper.findProductsVo(products);
        if (productVoList!=null) {
            productVoList.forEach(e -> {
                Integer id = e.getId();
                //根据产品id查找images
                Images images = new Images();
                images.setProductsId(id);
                List<Images> imagesList = imagesMapper.findImages(images);
                //根据产品id查找labels
                Labels labels = new Labels();
                labels.setProductsId(id);
                List<Labels> labelsList = labelsMapper.findLabels(labels);
                //根据产品id查询materials
                final MaterialsVo materialsVo = new MaterialsVo();
                materialsVo.setProductsId(id);
                List<MaterialsVo> materialsVoList = materialsMapper.findMaterials(materialsVo);
                e.setImagesList(imagesList);
                e.setLabelsList(labelsList);
                e.setMaterialsVoList(materialsVoList);
            });
        }
        return productVoList;
    }

}
